from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
import re
import time
import requests
import urllib3
import MySQLdb
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from lxml import etree

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


class Jy_jd(object):

    def __init__(self):
        options = Options()
        options.add_argument('--headless')
        self.chrome = Chrome(options=options)
        self.chrome.get(
            'https://huodong.ctrip.com/things-to-do/list-7/city?citytype=dt&id=7&name=%E8%A5%BF%E5%AE%89&keyword=&pshowcode=Ticket2&bookingtransactionid=1704611673193_8801')
        time.sleep(3)
        self.page = 1
        self.headers = {
            'cookie': 'Session=SmartLinkCode=U155952&SmartLinkKeyWord=&SmartLinkQuary=&SmartLinkHost=&SmartLinkLanguage=zh; _RSG=KqK3qETfa143fOqQl4rFXB; _RDG=282f24100640c82731283334fcc3364464; _RGUID=4064a5d3-b40d-4d14-b84f-d44bdad18a43; Union=OUID=index&AllianceID=4897&SID=155952&SourceID=&createtime=1600831032&Expires=1601435831593; MKT_OrderClick=ASID=4897155952&AID=4897&CSID=155952&OUID=index&CT=1600831031597&CURL=https%3A%2F%2Fwww.ctrip.com%2F%3Fsid%3D155952%26allianceid%3D4897%26ouid%3Dindex&VAL={"pc_vid":"1600831028743.427gzc"}; MKT_CKID=1600831031634.5olt5.f6pj; MKT_CKID_LMT=1600831031635; _ga=GA1.2.248639397.1600831032; _gid=GA1.2.1954297618.1600831032; MKT_Pagesource=PC; GUID=09031031210931119554; nfes_isSupportWebP=1; appFloatCnt=1; nfes_isSupportWebP=1; ASP.NET_SessionSvc=MTAuNjAuMzUuMTQ2fDkwOTB8amlucWlhb3xkZWZhdWx0fDE1ODkwMDMyMjQ5NDI; U_TICKET_SELECTED_DISTRICT_CITY=%7B%22value%22%3A%7B%22districtid%22%3A%22835%22%2C%22districtname%22%3A%22%E6%8F%AD%E9%98%B3%22%2C%22isOversea%22%3Anull%7D%2C%22createTime%22%3A1600847243848%2C%22updateDate%22%3A1600847243848%7D; _RF1=113.118.204.141; _gat=1; _pd=%7B%22r%22%3A1%2C%22d%22%3A614%2C%22_d%22%3A613%2C%22p%22%3A634%2C%22_p%22%3A20%2C%22o%22%3A655%2C%22_o%22%3A21%2C%22s%22%3A668%2C%22_s%22%3A13%7D; _bfa=1.1600831028743.427gzc.1.1600843833503.1600847244099.5.49.10650038368; _bfs=1.30; _bfi=p1%3D290510%26p2%3D290510%26v1%3D49%26v2%3D48; _jzqco=%7C%7C%7C%7C1600831031803%7C1.1555887407.1600831031625.1600849509140.1600849530503.1600849509140.1600849530503.0.0.0.19.19; __zpspc=9.4.1600846262.1600849530.14%232%7Cwww.baidu.com%7C%7C%7C%25E6%2590%25BA%25E7%25A8%258B%7C%23',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
        }

    def open(self):
        self.con = MySQLdb.connect(host="127.0.0.1", port=3306, user='root', password="root",
                                   db="lvyou", charset='utf8')
        self.cursor = self.con.cursor()

    def close(self):
        self.con.commit()
        self.con.close()

    def insert(self, title, aj, score, reviews, hot, address, jieshao, kfsj, ydzc, fwss):
        sql = "insert into table_data (title, aj, score, reviews, hot, address, " \
              "jieshao, kfsj, ydzc, fwss) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        self.cursor.execute(sql, [title, aj, score, reviews, hot, address, jieshao, kfsj, ydzc, fwss])

    def queryCount(self):
        sql = "select count(*) from table_data"
        self.cursor.execute(sql)
        self.data = self.cursor.fetchall()[0]
        if self.data == 1000:
            self.con.close()

    def query(self, title):
        sql = "select title from table_data where title = (%s)"
        self.cursor.execute(sql, [title])
        rows = self.cursor.fetchall()
        self.rows = len(rows)
        if len(rows) > 0:
            print("数据库已存在记录！")
            self.con.close()


if __name__ == '__main__':
    jy_jd = Jy_jd()
    # jy_jd.get_url()
